#include "student.h"
#include <QDebug>
student::student()
{
    this->file1=new file;
    this->setM=new modelSet;
}

void student::setInfo(QString name, QString id, QString grade, QString stuclass, QString college, QString major, QString password, QStringList course)
{
    this->person::setInfo(name,id);
    this->stuGrade=grade;
    this->stuClass=stuclass;
    this->stuCollege=college;
    this->stuMajor=major;
    this->stuPassword=password;
    this->stuCourse=course;
}

void student::setScore(QString name,QString id,QStringList score)
{
    this->person::setInfo(name,id);
    this->stuScore=score;
}

void student::loadAboutInfo()
{
    //加载全校课程信息
    QStringList _line1;
    this->file1->readFile("course.csv",_line1);
    for(int i=0;i<_line1.length();i++)
    {
        QString line=_line1.at(i);
        line=line.trimmed();
        QStringList linesplit=line.split(",");
        course a;
        a.setCourseInfo(linesplit.at(0),linesplit.at(1),linesplit.at(2),linesplit.at(3),linesplit.at(4),linesplit.at(5),linesplit.at(6),linesplit.at(7),linesplit.at(8),linesplit.at(9),linesplit.at(10),linesplit.at(11),linesplit.at(12));
        allCourse.push_back(a);
    }
    //加载个人已选课程信息到类中
    for(int i=0;i<stuCourse.length();i++)
    {
        myCourse.push_back(matchCourseWithId(stuCourse.at(i),allCourse));
    }
}

QString student::getCollege() const
{
    return this->stuCollege;
}

QStringList student::getSelectedCourse() const
{
    return this->stuCourse;
}

//返回当前学生的所有信息
QStringList student::getAllInfo() const
{
    QStringList allInfo;
    allInfo.append(getName());
    allInfo.append(getId());
    allInfo.append(stuPassword);
    allInfo.append(stuGrade);
    allInfo.append(stuClass);
    allInfo.append(stuCollege);
    allInfo.append(stuMajor);
    allInfo.append(this->stuCourse);
    return allInfo;
}

student *student::next()
{
    return (this+1);
}

bool student::selectCourse(QString courseSelectId,QString courseName,QString courseTime)
{
    for(int i=0;i<myCourse.length();i++)
    {
        if(myCourse.at(i).getCourseSelectId()==courseSelectId||myCourse.at(i).getCourseTime()==courseTime||myCourse.at(i).getCourseName()==courseName)
        {
            return false;
        }
    }
    matchCourseWithId(courseSelectId,allCourse).peoplePlusOne();
    addCourse(courseSelectId);
    myCourse.push_back(matchCourseWithId(courseSelectId,allCourse));
    return true;
}

void student::checkAllCourse(QStandardItemModel *model)
{
    model->clear();
    setM->setCourseTitle(model);
    for(int i=0;i<allCourse.length();i++)
    {
        allCourse.at(i).displayThisCourse(model,i);
    }
}

void student::checkMyScore(QStandardItemModel *model, int curRow) const
{
    QStringList line;
    line.append(this->getName());
    line.append(this->getId());
    line.append(this->stuScore);
    this->setM->setScoreTitle(model);
    this->setM->setScoreItem(model,line,curRow);
}

void student::checkMyInfo(QStandardItemModel *model, int curRow) const
{
    QStringList line;
    line.append(this->getName());
    line.append(this->getId());
    line.append(this->stuGrade);
    line.append(this->stuClass);
    line.append(this->stuCollege);
    line.append(this->stuMajor);
    //line.append(this->stuCourse);
    this->setM->setStudentTitle(model);
    this->setM->setStudentItem(model,line,curRow);
}

bool student::modifyPassword(QString oldPassword, QString newPassword)
{
    if(oldPassword==this->stuPassword)
    {
        if(newPassword.length()<6)
        {
            return false;
        }
        else
        {
            this->stuPassword=newPassword;
            return  true;
        }
    }
    else
    {
        return false;
    }
}

void student::saveAllInfo()
{
    this->file1->modifyFile("student",getAllInfo());
    for(int i=0;i<allCourse.length();i++)
    {
        //一行一行的写入课程信息
        this->file1->writeFile("course",allCourse.at(i).getAllInfo());
    }
    this->file1->replaceFile("course","course_temp");
}

course &student::matchCourseWithId(const QString courseSelectId, QVector<course> &course)
{
    for(int i=0;i<course.length();i++)
    {
        if(course.at(i).getCourseSelectId()==courseSelectId)
        {
            return course[i];
        }
    }
}

void student::addCourse(QString id)
{
    stuCourse.append(id);
}

void student::removeCourse(QString id)
{
    for(int i=0;i<stuCourse.length();i++)
    {
        if(stuCourse.at(i)==id)
        {
            stuCourse.removeAt(i);
        }
    }
}

void student::checkMyCourse(QStandardItemModel *model)
{
    this->setM->setCourseTitle(model);
    for(int i=0;i<myCourse.length();i++)
    {
        myCourse.at(i).displayThisCourse(model,i);
    }
}

bool student::giveUpCourse(QString courseSelectId)
{
    for(int i=0;i<myCourse.length();i++)
    {
        if(myCourse.at(i).getCourseSelectId()==courseSelectId)
        {
            myCourse.removeAt(i);
            matchCourseWithId(courseSelectId,allCourse).peopleMinOne();
            removeCourse(courseSelectId);
            return true;
        }
    }
    return false;
}

QString student::getAvg(){
    return stuScore[4];
}
